package server_logic;

import java.sql.Date;
import java.util.Vector;

import mysqlCnnection.CampaignUpdate;
import mysqlCnnection.CatalogueUpdate;
import mysqlCnnection.CustomersUpdate;
import mysqlCnnection.IlegalMySQLPasswordORUserNameException;
import mysqlCnnection.Reports;
import mysqlCnnection.SellsMenUpdate;

/**
 * This class analyze client request and make the answer to client 
 * @author matan
 * @author sharon
 * @author ido
 * @author michael 
 */
public class ConnectionToUser 
{
	
	private String[] clientmessage = null;
	private Object messagetoclient = null;
	
	/**
	 * @param clientmessage
	 */
	public ConnectionToUser(String[] clientmessage) 
	{
		super();
		this.clientmessage = clientmessage;
	}

	/**
	 * @return the clientmessage
	 */
	public String[] getClientmessage() {
		return clientmessage;
	}
	
	/**
	 * This method update (messagetoclient) to product details or null on error 
	 */
	private void getProductDetails()
	{
		try
		{
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			String[] productdetails = catalogueupdate.searchForProduct(Long.parseLong(clientmessage[1]));
			if (productdetails != null)
			{
				messagetoclient = productdetails;			
			}
		}catch(Exception e)	{};		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if product details successfully inserted,
	 * else to "IlegalCataloguenumber" 
	 */
	private void insertProductDetails()
	{
		Server_Product product = new Server_Product(0, null, null, 0);
		try
		{
			product.setPrice(Double.parseDouble(clientmessage[1]));
			product.setName(clientmessage[2]);
			product.setDescription(clientmessage[3]);
			product.setCataloguenumber(Long.parseLong(clientmessage[4]));			
			if (product.insertToDataBase())
			{
				messagetoclient = "OK";
			}
			else
				messagetoclient = "IlegalCataloguenumber";
		}catch(Exception e){};	
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if product details successfully updated,
	 * else to "IlegalCataloguenumber" 
	 */
	private void updateProductDetails()
	{
		Server_Product product = new Server_Product(0, null, null, 0);
		try
		{
			product.setPrice(Double.parseDouble(clientmessage[1]));
			product.setName(clientmessage[2]);
			product.setDescription(clientmessage[3]);
			product.setCataloguenumber(Long.parseLong(clientmessage[4]));			
			if (product.updateProduct())
			{
				messagetoclient = "OK";
			}
			else
				messagetoclient = "IlegalCataloguenumber";
		}catch(Exception e){};		
	}
	
	
	/**
	 * This method update (messagetoclient) to "OK" if product successfully remove 
	 * from data base, else to "ERROR" 
	 */
	private void removeProduct()
	{
		try
		{
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			messagetoclient = catalogueupdate.RemoveProduct(Long.parseLong(clientmessage[1]));
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = "ERROR";
		}
		catch(NumberFormatException e)	
		{
			messagetoclient = "ERROR";
		};	
	}
	
	/**
	 * This method update (messagetoclient) to Long array of all products ID  
	 * or to null on error 
	 */
	private void loadAllProductsID()
	{
		try
		{
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			messagetoclient = catalogueupdate.loadAllProductID();
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of all market sectors ID  
	 * or to null on error 
	 */
	private void loadAllClientsMarket()
	{
		try
		{
			CampaignUpdate campaignUpdate = new CampaignUpdate();
			messagetoclient = campaignUpdate.loadAllClientsMarketSectors();
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to "OK" campaign successfully added  
	 * to data base, or to "IlegalID" on SQL error, else to null. 
	 */
	private void createCampaign()
	{
		try
		{
			CampaignUpdate campaignUpdate = new CampaignUpdate();
			String name, sector, message;
			long pID, cID;
			cID = Long.parseLong(clientmessage[1]);
			name = clientmessage[2];
			sector = clientmessage[3];
			message = clientmessage[4];
			pID = Long.parseLong(clientmessage[5]);
			messagetoclient = campaignUpdate.createCampaign(cID,name,sector,message,pID);
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of all customersID  
	 * or to null on error 
	 */
	private void loadAllCustomers()
	{
		try
		{	
			CustomersUpdate customersupdate = new CustomersUpdate();
			messagetoclient = customersupdate.loadAllCustomers();
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of customers details
	 * or to null on error 
	 */
	private void loadCustomer()
	{
		try
		{	
			CustomersUpdate customersupdate = new CustomersUpdate();
			messagetoclient = customersupdate.loadCustomer(Long.parseLong(clientmessage[1]));
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of area details
	 * or to null on error 
	 */
	private void loadAllAreas()
	{
		try
		{	
			CustomersUpdate customersupdate = new CustomersUpdate();
			messagetoclient = customersupdate.loadAllAreas();			
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if customer successfully added  
	 * to data base, or to "IlegalID" on SQL error, else to null. 
	 */
	private void addCustomer()
	{
		try
		{	
			CustomersUpdate customersupdate = new CustomersUpdate();
			long cid = Long.parseLong(clientmessage[1]);
			String name = clientmessage[2];
			int age = Integer.parseInt(clientmessage[3]);
			int area = Integer.parseInt(clientmessage[4]);
			String phone = clientmessage[5];
			if (customersupdate.addCustomer(cid, name, age, area,phone,1))
				messagetoclient = "OK";
			else
				messagetoclient = "ERROR";
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if customer info successfully update  
	 * in data base, else to null. 
	 */
	private void updateCustomer()
	{
		try
		{	
			CustomersUpdate customersupdate = new CustomersUpdate();
			long cid = Long.parseLong(clientmessage[1]);
			String name = clientmessage[2];
			int age = Integer.parseInt(clientmessage[3]);
			int area = Integer.parseInt(clientmessage[4]);
			String phone = clientmessage[5];
			if (customersupdate.updateCustomer(cid, name, age, area,phone))
				messagetoclient = "OK";
			else
				messagetoclient = "ERROR";
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of campaignID
	 * or to null on error 
	 */
	private void loadAllCampaigns()
	{
		try
		{	
			CampaignUpdate campaignupdate = new CampaignUpdate();
			messagetoclient = campaignupdate.loadAllCampaigns();			
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of campaign details
	 * or to null on error 
	 */
	private void loadCampaign()
	{
		try
		{	
			CampaignUpdate campaignupdate = new CampaignUpdate();
			messagetoclient = campaignupdate.loadCampaign(Long.parseLong(clientmessage[1]));
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
		
	/**
	 * This method update (messagetoclient) to "OK" if campaign successfully updated  
	 * in data base, or to null on SQL error. 
	 */
	private void updateCampaign()
	{
		try
		{
			CampaignUpdate campaignUpdate = new CampaignUpdate();
			String name, sector, message;
			long pID, cID;
			cID = Long.parseLong(clientmessage[1]);
			name = clientmessage[2];
			sector = clientmessage[3];
			message = clientmessage[4];
			pID = Long.parseLong(clientmessage[5]);
			messagetoclient = campaignUpdate.updateCampaign(cID,name,sector,message,pID);
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of section details
	 * or to null on error 
	 */
	private void loadAllSection()
	{
		try
		{	
			CustomersUpdate customersupdate = new CustomersUpdate();
			messagetoclient = customersupdate.loadAllSection();			
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of all sections product 
	 * details or to null on error. 
	 */
	private void loadSectionsProduct()
	{
		try
		{	
			int sID = Integer.parseInt(clientmessage[1]);
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			messagetoclient = catalogueupdate.loadSectionsProduct(sID);	
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
		
	/**
	 * This method update (messagetoclient) to "OK" if the authorization successfully
	 *  added to data base, or to null on SQL error. 
	 */
	private void addAuthorization()
	{
		try
		{	
			int sectionID = Integer.parseInt(clientmessage[1]);
			long customerID = Long.parseLong(clientmessage[2]);
			CustomersUpdate customersupdate = new CustomersUpdate();
			if (customersupdate.addAuthorization(sectionID, customerID))
				messagetoclient = "OK";
			else
				messagetoclient = "ERROR";
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of all clients 
	 * authorizations details or to null on error. 
	 */
	private void loadCustomerAuthorization()
	{
		try
		{	
			long customerID = Long.parseLong(clientmessage[1]);
			CustomersUpdate customersupdate = new CustomersUpdate();
			messagetoclient = customersupdate.loadCustomerAuthorization(customerID);	
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) toto "OK" if the area successfully
	 *  added to data base, or to null on SQL error.  
	 */
	private void addArea()
	{
		int areaID = Integer.parseInt(clientmessage[1]);
		String areaName = clientmessage[2];
		CustomersUpdate customersupdate = null;
		try
		{	
			customersupdate = new CustomersUpdate();			
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
			return;
		}
		
		if (customersupdate.addArea(areaID, areaName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the area successfully
	 *  added to data base, or to null on SQL error.  
	 */
	private void updateArea()
	{
		int areaID = Integer.parseInt(clientmessage[1]);
		String areaName = clientmessage[2];
		CustomersUpdate customersupdate = null;
		try
		{	
			customersupdate = new CustomersUpdate();			
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
			return;
		}
		
		if (customersupdate.updateArea(areaID, areaName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to array of String of all catalog sections
	 * authorizations details or to null on error. 
	 */
	private void loadAllCategorySections()
	{
		try
		{	
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			messagetoclient = catalogueupdate.loadAllCategorySections();	
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the section successfully
	 *  added to data base, or to null on SQL error.  
	 */
	private void addCatalogueSection()
	{
		int sectionID=0;
		CatalogueUpdate catalogueupdate = null;
		try
		{	
			sectionID = Integer.parseInt(clientmessage[1]);
			catalogueupdate = new CatalogueUpdate();			
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
			return;
		}catch (NumberFormatException except)
		{
			messagetoclient = null;
			return;
		}
		String sectionName = clientmessage[2];		
		if (catalogueupdate.addCatalogueSection(sectionID, sectionName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the section successfully
	 *  updated in data base, or to null on SQL error.  
	 */
	private void updateCatalogueSection()
	{
		int sectionID=0;
		CatalogueUpdate catalogueupdate = null;
		try
		{	
			sectionID = Integer.parseInt(clientmessage[1]);
			catalogueupdate = new CatalogueUpdate();			
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
			return;
		}catch (NumberFormatException except)
		{
			messagetoclient = null;
			return;
		}
		String sectionName = clientmessage[2];		
		if (catalogueupdate.updateCatalogueSection(sectionID, sectionName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the section successfully
	 *  remove from data base, or to null on SQL error.  
	 */
	private void removeCatalogueSection()
	{
		int sectionID=0;
		CatalogueUpdate catalogueupdate = null;
		try
		{	
			sectionID = Integer.parseInt(clientmessage[1]);
			catalogueupdate = new CatalogueUpdate();			
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
			return;
		}catch (NumberFormatException except)
		{
			messagetoclient = null;
			return;
		}
		if (catalogueupdate.removeCatalogueSection(sectionID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the type successfully
	 *  remove from data base, or to null on SQL error.  
	 */
	private void removeCatalogueType()
	{
		int typeID=0;
		CatalogueUpdate catalogueupdate = null;
		try
		{	
			typeID = Integer.parseInt(clientmessage[1]);
			catalogueupdate = new CatalogueUpdate();			
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
			return;
		}catch (NumberFormatException except)
		{
			messagetoclient = null;
			return;
		}
		if (catalogueupdate.removeCatalogueType(typeID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the type successfully
	 *  updated in data base, or to null on SQL error.  
	 */
	private void updateCatalogueType()
	{
		int typeID=0;
		CatalogueUpdate catalogueupdate = null;
		try
		{	
			typeID = Integer.parseInt(clientmessage[1]);
			catalogueupdate = new CatalogueUpdate();			
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
			return;
		}catch (NumberFormatException except)
		{
			messagetoclient = null;
			return;
		}
		String typeName = clientmessage[2];		
		if (catalogueupdate.updateCatalogueType(typeID, typeName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the type successfully
	 *  added to data base, or to null on SQL error.  
	 */
	private void addCataloguetype()
	{
		int typeID=0;
		CatalogueUpdate catalogueupdate = null;
		try
		{	
			typeID = Integer.parseInt(clientmessage[1]);
			catalogueupdate = new CatalogueUpdate();			
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
			return;
		}catch (NumberFormatException except)
		{
			messagetoclient = null;
			return;
		}
		String typeName = clientmessage[2];		
		if (catalogueupdate.addCataloguetype(typeID, typeName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to array of String of all catalog types
	 * details or to null on error. 
	 */
	private void loadAllCategoryType()
	{
		try
		{	
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			messagetoclient = catalogueupdate.loadAllCategoryType();	
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String array of all type product 
	 * details or to null on error. 
	 */
	private void loadTypeProduct()
	{
		try
		{	
			int tID = Integer.parseInt(clientmessage[1]);
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			messagetoclient = catalogueupdate.loadTypeProduct(tID);	
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the product successfully 
	 * remove from the type, or to null on SQL error.  
	 */
	private void removeProductFromType()
	{
		int typeID=0;
		long productID=0;
		CatalogueUpdate catalogueupdate = null;
		try
		{	
			typeID = Integer.parseInt(clientmessage[1]);
			productID = Long.parseLong(clientmessage[2]);
			catalogueupdate = new CatalogueUpdate();			
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
			return;
		}catch (NumberFormatException except)
		{
			messagetoclient = null;
			return;
		}
		if (catalogueupdate.removeProductFromType(typeID, productID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the product successfully 
	 * added to the type, or to null on SQL error.  
	 */
	private void addProductToType()
	{
		int typeID=0;
		long productID=0;
		CatalogueUpdate catalogueupdate = null;
		try
		{	
			typeID = Integer.parseInt(clientmessage[1]);
			productID = Long.parseLong(clientmessage[2]);
			catalogueupdate = new CatalogueUpdate();			
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
			return;
		}catch (NumberFormatException except)
		{
			messagetoclient = null;
			return;
		}
		if (catalogueupdate.addProductToType(typeID, productID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to String array of all section types 
	 * details or to null on error. 
	 */
	private void loadAllSectionType()
	{
		try
		{	
			int cectionID = Integer.parseInt(clientmessage[1]);
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			messagetoclient = catalogueupdate.loadAllSectionType(cectionID);	
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the type successfully 
	 * remove from the section, or to null on SQL error.  
	 */
	private void removeTypeFromSection()
	{
		int typeID=0;
		int sectionID=0;
		CatalogueUpdate catalogueupdate = null;
		try
		{	
			sectionID = Integer.parseInt(clientmessage[1]);
			typeID = Integer.parseInt(clientmessage[2]);
			catalogueupdate = new CatalogueUpdate();			
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
			return;
		}catch (NumberFormatException except)
		{
			messagetoclient = null;
			return;
		}
		if (catalogueupdate.removeTypeFromSection(sectionID, typeID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if the type successfully 
	 * added to the section, or to null on SQL error.  
	 */
	private void addTypeToSection()
	{
		int typeID=0;
		int sectionID=0;
		CatalogueUpdate catalogueupdate = null;
		try
		{	
			sectionID = Integer.parseInt(clientmessage[1]);
			typeID = Integer.parseInt(clientmessage[2]);
			catalogueupdate = new CatalogueUpdate();			
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
			return;
		}catch (NumberFormatException except)
		{
			messagetoclient = null;
			return;
		}
		if (catalogueupdate.addTypeToSection(sectionID, typeID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if activeCampaign successfully updated  
	 * in data base, or to null on SQL error. 
	 */
	private void updateActiveCampaign()
	{
		long activeCampaignID;
		String status, year, month, day, tmp;
		try
		{
        	CampaignUpdate campaignUpdate = new CampaignUpdate();
			activeCampaignID = Long.parseLong(clientmessage[1]);
			tmp = clientmessage[2];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date startdate = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
	        tmp = clientmessage[3];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date endate = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
			status = clientmessage[4];			
			messagetoclient = campaignUpdate.updateActiveCampaign(activeCampaignID, startdate, endate, status );
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to "OK" if activeCampaign successfully 
	 * inserted to data base, or to null on SQL error. 
	 */
	private void saveAtiveCampaign()
	{
		long activeCampaignID, campaignID;
		String status, tmp, year, month, day;
		try
		{
        	CampaignUpdate campaignUpdate = new CampaignUpdate();
			activeCampaignID = Long.parseLong(clientmessage[1]);
			tmp = clientmessage[2];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date startdate = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
	        tmp = clientmessage[3];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date endate = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
	        status = clientmessage[4];
	        campaignID =  Long.parseLong(clientmessage[5]);
			messagetoclient = campaignUpdate.saveAtiveCampaign(activeCampaignID, startdate, endate, status, campaignID);
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to activeCampaign details as Object[] 
	 */
	private void loadActiveCampaign()
	{
		long activeCampaignID;
		try
		{
        	CampaignUpdate campaignUpdate = new CampaignUpdate();
			activeCampaignID = Long.parseLong(clientmessage[1]);
			messagetoclient = campaignUpdate.loadActiveCampaign(activeCampaignID);
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to activeCampaign ID as Vector<Long> 
	 */
	private void loadAllactiveCampaigns()
	{
		try
		{
        	CampaignUpdate campaignUpdate = new CampaignUpdate();
			messagetoclient = campaignUpdate.loadAllactiveCampaigns();
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to customers ID as Vector<Long> 
	 */
	private void loadAllclientAtSector()
	{
		try
		{
			CustomersUpdate customersUpdate = new CustomersUpdate();
			int sector = Integer.parseInt(clientmessage[1]);
			messagetoclient = customersUpdate.loadAllclientAtSector(sector);
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to customers ID as Vector<Long> 
	 */
	private void loadAllClientForSellsmen()
	{
		try
		{
			SellsMenUpdate sellsMenUpdate = new SellsMenUpdate();
			messagetoclient = sellsMenUpdate.loadAllClientForSellsmen(clientmessage[1]);
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to sellsMen ID as Vector<Long> 
	 */
	private void loadAllSellsMem()
	{
		try
		{
			SellsMenUpdate sellsmenupdate = new SellsMenUpdate();
 			messagetoclient = sellsmenupdate.loadAllSellsMem();
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to sellsMen ID as Vector<Long> 
	 */
	private void updateSellsMenSchedule()
	{
		try
		{
			SellsMenUpdate sellsmenupdate = new SellsMenUpdate();
			String schedule = clientmessage[1];
			long activeID = Long.parseLong(clientmessage[2]);
 			if (sellsmenupdate.updateSellsMenSchedule(schedule, activeID))
 				messagetoclient = "OK";
 			else
 				messagetoclient = "ERROR";
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to sellsMen ID as Vector<Long> 
	 */
	private void loadActiveCampaignForClient()
	{
		try
		{
			SellsMenUpdate sellsmenupdate = new SellsMenUpdate();
			long cID = Long.parseLong(clientmessage[1]);
			messagetoclient = sellsmenupdate.loadActiveCampaignForClient(cID);
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to sellsMen ID as Vector<Long> 
	 */
	private void delayCampaign()
	{
		String status, tmp, year, month, day;
		try
		{
			SellsMenUpdate sellsmenupdate = new SellsMenUpdate();
			long cID = Long.parseLong(clientmessage[1]);
			long active = Long.parseLong(clientmessage[2]);
			String message = clientmessage[3];
			tmp = clientmessage[4];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date date = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
			if (sellsmenupdate.delayCampaign(cID,active ,message, date))
				messagetoclient = "OK";
			else
				messagetoclient = "ERROR";
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
		
	/**
	 * This method update (messagetoclient) to String on success "OK", "ERROR" on error 
	 */
	private void purchasesCampaign()
	{
		String status, tmp, year, month, day;
		try
		{
			SellsMenUpdate sellsmenupdate = new SellsMenUpdate();
			long cID = Long.parseLong(clientmessage[1]);
			long active = Long.parseLong(clientmessage[2]);
			tmp = clientmessage[3];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date date = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
			if (sellsmenupdate.purchasesCampaign(cID,active,date))
				messagetoclient = "OK";
			else
				messagetoclient = "ERROR";
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String on success "OK", "ERROR" on error 
	 */
	private void analyzesSystem()
	{
		try
		{
			CustomersUpdate customersUpdate = new CustomersUpdate();
			messagetoclient = customersUpdate.analyzesSystem();
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String of response report 
	 */
	private void getResponseReport()
	{
		try
		{
			long activeID = Long.parseLong(clientmessage[1]);
			Reports reports = new Reports();
			messagetoclient = reports.getResponseReport(activeID);
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method update (messagetoclient) to String of response report 
	 */
	private void customerReport()
	{
		try
		{
			Reports reports = new Reports();
			messagetoclient = reports.customerReport();
		}catch (IlegalMySQLPasswordORUserNameException except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This method return (messagetoclient): message that server send to client
	 */
	public synchronized Object getMessageToClient()
	{
		if (clientmessage[0].equals("LoadProduct"))
		{
			getProductDetails();
			return messagetoclient;
		}
		if (clientmessage[0].equals("AddProduct"))
		{
			insertProductDetails();
			return messagetoclient;
		}
		if (clientmessage[0].equals("UpdateProduct"))
		{
			updateProductDetails();
			return messagetoclient;
		}
		if (clientmessage[0].equals("RemoveProduct"))
		{
			removeProduct();
			return messagetoclient;
		}
		if (clientmessage[0].equals("LoadAllProductsID"))
		{
			loadAllProductsID();
			return messagetoclient;
		}	
		
		if (clientmessage[0].equals("LoadAllClientsMarket"))
		{
			loadAllClientsMarket();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("CreateCampaign"))
		{
			createCampaign();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("LoadAllCustomers"))
		{
			loadAllCustomers();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("LoadCustomer"))
		{
			loadCustomer();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("LoadAllAreas"))
		{
			loadAllAreas();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("AddCustomer"))
		{
			addCustomer();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("UpdateCustomer"))
		{
			updateCustomer();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("LoadAllCampaigns"))
		{
			loadAllCampaigns();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("LoadCampaign"))
		{
			loadCampaign();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("UpdateCampaign"))
		{
			updateCampaign();
			return messagetoclient;
		}
		if (clientmessage[0].equals("LoadAllSection"))
		{
			loadAllSection();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("LoadSectionsProduct"))
		{
			loadSectionsProduct();
			return messagetoclient;
		}
		if (clientmessage[0].equals("AddAuthorization"))
		{
			addAuthorization();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("LoadCustomerAuthorization"))
		{
			loadCustomerAuthorization();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("AddArea"))
		{
			addArea();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("UpdateArea"))
		{
			updateArea();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("LoadAllCategorySections"))
		{
			loadAllCategorySections();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("AddCatalogueSection"))
		{
			addCatalogueSection();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("UpdateCatalogueSection"))
		{
			updateCatalogueSection();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("RemoveCatalogueSection"))
		{
			removeCatalogueSection();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("RemoveCatalogueType"))
		{
			removeCatalogueType();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("UpdateCatalogueType"))
		{
			updateCatalogueType();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("AddCataloguetype"))
		{
			addCataloguetype();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("LoadAllCategoryType"))
		{
			loadAllCategoryType();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("LoadTypeProduct"))
		{
			loadTypeProduct();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("RemoveProductFromType"))
		{
			removeProductFromType();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("AddProductToType"))
		{
			addProductToType();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("LoadAllSectionType"))
		{
			loadAllSectionType();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("RemoveTypeFromSection"))
		{
			removeTypeFromSection();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("AddTypeToSection"))
		{
			addTypeToSection();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("UpdateActiveCampaign"))
		{
			updateActiveCampaign();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("SaveAtiveCampaign"))
		{
			saveAtiveCampaign();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("LoadActiveCampaign"))
		{
			loadActiveCampaign();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("LoadAllactiveCampaigns"))
		{
			loadAllactiveCampaigns();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("LoadAllclientAtSector"))
		{
			loadAllclientAtSector();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("LoadAllSellsMem"))
		{
			loadAllSellsMem();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("UpdateSellsMenSchedule"))
		{
			updateSellsMenSchedule();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("loadAllClientForSellsmen"))
		{
			loadAllClientForSellsmen();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("loadActiveCampaignForClient"))
		{
			loadActiveCampaignForClient();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("delayCampaign"))
		{
			delayCampaign();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("purchasesCampaign"))
		{
			purchasesCampaign();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("getResponseReport"))
		{
			getResponseReport();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("analyzesSystem"))
		{
			analyzesSystem();
			return messagetoclient;
		}	
		if (clientmessage[0].equals("customerReport"))
		{
			customerReport();
			return messagetoclient;
		}	
		
		
		return null;
	}


	
	
}
